/* ************************************************************************************* 
 * CREATED BY:          LY HUNG SON
 * CREATED DATE:        9/5/2008
 * PROJECT:             MCM
 * PURPOSE:             Paging object
 * MODIFIED BY:         None
 * MODIFIED DATE:       None
 * MODIFIED REASON:     None
 * NOTES:               None
 **************************************************************************************/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// EventArgs for PageSelectedIndexChange Event.
/// </summary>
public class PageSelectedIndexChange : EventArgs
{
    private int _SelectedPageIndex;

    public int SelectedPageIndex
    {
        get { return _SelectedPageIndex; }
        set { _SelectedPageIndex = value; }
    }

    public PageSelectedIndexChange(int SelectedPageIndex)
    {
        this.SelectedPageIndex = SelectedPageIndex;
    }
}
/// <summary>
/// Enum value
/// </summary>
public enum PagingNumberType { NumberList, CurrentPageAndTotalPages, CurrentRecordAndTotalRecords, NotUsed }
public enum PagingNavigatorType { ImageButton, Hyperlink }
public enum PagingDisplayNavigatorType { FirstPrevAndNextLast, PrevAndNext, NotUsed }

public partial class Modules_WebsiteFrontSub_wucPaging : System.Web.UI.UserControl
{
    public delegate void PageSelectedIndexChangeEventHandler(object sender, PageSelectedIndexChange e);
    public event PageSelectedIndexChangeEventHandler PageSelectedIndexChanged;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.BindPaging();
        }
    }

    #region Styling your control ----------------------

    #region PagingObjectContainer style

    public string PagingObjectContainerCssClass
    {
        get { return PagingObjectContainer.CssClass; }
        set { PagingObjectContainer.CssClass = value; }
    }

    #endregion

    #region NavigatorObjectFirst setting and style

    // Container
    public string NavigatorObjectFirstContainerCssClass
    {
        get { return NavigatorObjectFirstContainer.CssClass; }
        set { NavigatorObjectFirstContainer.CssClass = value; }
    }

    // Image button
    public string NavigatorObjectFirstImageButtonCssClass
    {
        get { return ibtnFirst.CssClass; }
        set { ibtnFirst.CssClass = value; }
    }

    public string NavigatorObjectFirstImageButtonImageUrl
    {
        get { return ibtnFirst.ImageUrl; }
        set { ibtnFirst.ImageUrl = value; }
    }

    // Link button
    public string NavigatorObjectFirstLinkCssClass
    {
        get { return lbtnFirst.CssClass; }
        set { lbtnFirst.CssClass = value; }
    }

    public string NavigatorObjectFirstLinkText
    {
        get { return lbtnFirst.Text; }
        set { lbtnFirst.Text = value; }
    }

    #endregion

    #region NavigatorObjectPrev setting and style

    // Container
    public string NavigatorObjectPrevContainerCssClass
    {
        get { return NavigatorObjectPrevContainer.CssClass; }
        set { NavigatorObjectPrevContainer.CssClass = value; }
    }

    // Image button
    public string NavigatorObjectPrevImageButtonCssClass
    {
        get { return ibtnPrev.CssClass; }
        set { ibtnPrev.CssClass = value; }
    }

    public string NavigatorObjectPrevImageButtonImageUrl
    {
        get { return ibtnPrev.ImageUrl; }
        set { ibtnPrev.ImageUrl = value; }
    }

    // Link button
    public string NavigatorObjectPrevLinkCssClass
    {
        get { return lbtnPrev.CssClass; }
        set { lbtnPrev.CssClass = value; }
    }

    public string NavigatorObjectPrevLinkText
    {
        get { return lbtnPrev.Text; }
        set { lbtnPrev.Text = value; }
    }

    #endregion

    #region NavigatorObjectNumericContainer setting and style

    // NavigatorObjectNumericContainer
    public string NavigatorObjectNumericContainerCssClass
    {
        get { return NavigatorObjectNumericContainer.CssClass; }
        set { NavigatorObjectNumericContainer.CssClass = value; }
    }

    #region NumericSectionNumericList style and setting

    // NumericSectionNumericList setting
    public string NumericSectionNumericListCssClass
    {
        get { return NumericSectionNumericList.CssClass; }
        set { NumericSectionNumericList.CssClass = value; }
    }

    public string NumericSectionNumericListItemCssClass
    {
        get { return NumericSectionNumericList.ItemStyle.CssClass; }
        set { NumericSectionNumericList.ItemStyle.CssClass = value; }
    }

    public string NumericSectionNumericListItemSelectedCssClass
    {
        get { return NumericSectionNumericList.SelectedItemStyle.CssClass; }
        set { NumericSectionNumericList.SelectedItemStyle.CssClass = value; }
    }

    public string NumericSectionNumericListItemAlternatingCssClass
    {
        get { return NumericSectionNumericList.AlternatingItemStyle.CssClass; }
        set { NumericSectionNumericList.AlternatingItemStyle.CssClass = value; }
    }
    // Numeric link inside datalist
    public string NumericSectionNumericListHyperlinkCssClass
    {
        get { return Convert.ToString(this.ViewState["NumericSectionNumericListHyperlinkCssClass"]); }
        set { this.ViewState["NumericSectionNumericListHyperlinkCssClass"] = value; }
    }

    public string NumericSectionNumericListHyperlinkSelectedCssClass
    {
        get { return Convert.ToString(this.ViewState["NumericSectionNumericListHyperlinkSelectedCssClass"]); }
        set { this.ViewState["NumericSectionNumericListHyperlinkSelectedCssClass"] = value; }
    }

    public string NumericSectionNumericListItemSelectedContainerCssClass
    {
        get { return Convert.ToString(this.ViewState["NumericSectionNumericListItemSelectedContainerCssClass"]); }
        set { this.ViewState["NumericSectionNumericListItemSelectedContainerCssClass"] = value; }
    }

    public string NumericSectionNumericListItemContainerCssClass
    {
        get { return Convert.ToString(this.ViewState["NumericSectionNumericListItemContainerCssClass"]); }
        set { this.ViewState["NumericSectionNumericListItemContainerCssClass"] = value; }
    }

    #endregion

    #region NumericSectionCurrentTotalPage style and setting

    // NumericSectionCurrentTotalPage setting
    public string NumericSectionCurrentTotalPageContainerCssClass
    {
        get { return NumericSectionCurrentTotalPage.CssClass; }
        set { NumericSectionCurrentTotalPage.CssClass = value; }
    }
    // lblPagesCurrent setting
    public string PagesCurrentCssClass
    {
        get { return lblPagesCurrent.CssClass; }
        set { lblPagesCurrent.CssClass = value; }
    }
    // lblPagesCurrentTotalSeperator setting
    public string PagesCurrentTotalSeperatorCssClass
    {
        get { return lblPagesCurrentTotalSeperator.CssClass; }
        set { lblPagesCurrentTotalSeperator.CssClass = value; }
    }
    public string PagesCurrentTotalSeperatorText
    {
        get { return lblPagesCurrentTotalSeperator.Text; }
        set { lblPagesCurrentTotalSeperator.Text = value; }
    }
    // lblPagesTotal setting
    public string PagesTotalCssClass
    {
        get { return lblPagesTotal.CssClass; }
        set { lblPagesTotal.CssClass = value; }
    }

    #endregion

    #region NumericSectionCurrentTotalRecord style and setting

    // NumericSectionCurrentTotalRecord setting
    public string NumericSectionCurrentTotalRecordContainerCssClass
    {
        get { return NumericSectionCurrentTotalRecord.CssClass; }
        set { NumericSectionCurrentTotalRecord.CssClass = value; }
    }
    // lblRecordsCurrent setting
    public string RecordsCurrentCssClass
    {
        get { return lblRecordsCurrent.CssClass; }
        set { lblRecordsCurrent.CssClass = value; }
    }
    // lblRecordsCurrentTotalSeperator setting
    public string RecordsCurrentTotalSeperatorCssClass
    {
        get { return lblRecordsCurrentTotalSeperator.CssClass; }
        set { lblRecordsCurrentTotalSeperator.CssClass = value; }
    }
    public string RecordsCurrentTotalSeperatorText
    {
        get { return lblRecordsCurrentTotalSeperator.Text; }
        set { lblRecordsCurrentTotalSeperator.Text = value; }
    }
    // lblRecordsTotal setting
    public string RecordsTotalCssClass
    {
        get { return lblRecordsTotal.CssClass; }
        set { lblRecordsTotal.CssClass = value; }
    }

    #endregion

    #endregion

    #region NavigatorObjectNext setting and style

    // Container
    public string NavigatorObjectNextContainerCssClass
    {
        get { return NavigatorObjectNextContainer.CssClass; }
        set { NavigatorObjectNextContainer.CssClass = value; }
    }

    // Image button
    public string NavigatorObjectNextImageButtonCssClass
    {
        get { return ibtnNext.CssClass; }
        set { ibtnNext.CssClass = value; }
    }

    public string NavigatorObjectNextImageButtonImageUrl
    {
        get { return ibtnNext.ImageUrl; }
        set { ibtnNext.ImageUrl = value; }
    }

    // Link button
    public string NavigatorObjectNextLinkCssClass
    {
        get { return lbtnNext.CssClass; }
        set { lbtnNext.CssClass = value; }
    }

    public string NavigatorObjectNextLinkText
    {
        get { return lbtnNext.Text; }
        set { lbtnNext.Text = value; }
    }

    #endregion

    #region NavigatorObjectLast setting and style

    // Container
    public string NavigatorObjectLastContainerCssClass
    {
        get { return NavigatorObjectLastContainer.CssClass; }
        set { NavigatorObjectLastContainer.CssClass = value; }
    }

    // Image button
    public string NavigatorObjectLastImageButtonCssClass
    {
        get { return ibtnLast.CssClass; }
        set { ibtnLast.CssClass = value; }
    }

    public string NavigatorObjectLastImageButtonImageUrl
    {
        get { return ibtnLast.ImageUrl; }
        set { ibtnLast.ImageUrl = value; }
    }

    // Link button
    public string NavigatorObjectLastLinkCssClass
    {
        get { return lbtnLast.CssClass; }
        set { lbtnLast.CssClass = value; }
    }

    public string NavigatorObjectLastLinkText
    {
        get { return lbtnLast.Text; }
        set { lbtnLast.Text = value; }
    }

    #endregion

    #endregion

    #region Paging data section ------------------------

    #region Paging setting

    public PagingNumberType PagingNumberSetting
    {
        get { return (PagingNumberType)this.ViewState["PagingNumberType"]; }
        set { this.ViewState["PagingNumberType"] = value; }
    }

    public PagingNavigatorType PagingNavigatorSetting
    {
        get { return (PagingNavigatorType)this.ViewState["PagingNavigatorType"]; }
        set { this.ViewState["PagingNavigatorType"] = value; }
    }

    public PagingDisplayNavigatorType PagingDisplayNavigatorSetting
    {
        get { return (PagingDisplayNavigatorType)this.ViewState["PagingDisplayNavigatorType"]; }
        set { this.ViewState["PagingDisplayNavigatorType"] = value; }
    }

    #endregion

    #region Paging properties

    public int CurrentPage
    {
        get
        {
            if (this.ViewState["PagingCurrentPage"] != null)
                return Convert.ToInt32(this.ViewState["PagingCurrentPage"]);
            else return 0;
        }
        set
        {
            this.ViewState["PagingCurrentPage"] = value;
        }
    }

    public int TotalPage
    {
        get
        {
            if (this.ViewState["PagingTotalPage"] != null)
                return Convert.ToInt32(this.ViewState["PagingTotalPage"]);
            else return 0;
        }
        set
        {
            this.ViewState["PagingTotalPage"] = value;
        }
    }

    public int TotalRecord
    {
        get
        {
            if (this.ViewState["PagingTotalRecord"] != null)
                return Convert.ToInt32(this.ViewState["PagingTotalRecord"]);
            else return 0;
        }
        set
        {
            this.ViewState["PagingTotalRecord"] = value;
        }
    }

    public int PageSize
    {
        get
        {
            if (this.ViewState["PagingPageSize"] != null)
                return Convert.ToInt32(this.ViewState["PagingPageSize"]);
            else return 0;
        }
        set
        {
            this.ViewState["PagingPageSize"] = value;
        }
    }

    public int NumberOfPageDisplay
    {
        get
        {
            if (this.ViewState["NumberOfPageDisplay"] != null)
                return Convert.ToInt32(this.ViewState["NumberOfPageDisplay"]);
            else return 0;
        }
        set
        {
            this.ViewState["NumberOfPageDisplay"] = value;
        }
    }
    #endregion

    #endregion

    #region Paging data processing ---------------------

    #region Bind paging data

    public void BindPaging()
    {
        DataTable PagingTable = new DataTable();
        PagingTable.Columns.Add("PageIndex");
        PagingTable.Columns.Add("PageText");
        // get all viewstate param 
        int _CurrentPage = this.CurrentPage;
        int _TotalPage = this.TotalPage;
        int _PageSize = this.PageSize;
        int _NumberOfPageDisplay = this.NumberOfPageDisplay;

        // Setting for numeric datalist 
        // divice display number into two set
        int _PreviousPageSet = Convert.ToInt32(Math.Round(Convert.ToDecimal(_NumberOfPageDisplay / 2)));
        int _NextPageSet = _NumberOfPageDisplay - _PreviousPageSet;
        // check if current page is greater than the previous number set
        if (_CurrentPage > _PreviousPageSet)
        {
            if (_CurrentPage + _NextPageSet <= _TotalPage)
            {
                for (int i = _CurrentPage - _PreviousPageSet; i < _CurrentPage - _PreviousPageSet + _NumberOfPageDisplay; i++)
                {
                    DataRow PagingRow = PagingTable.NewRow();
                    PagingRow["PageIndex"] = i;
                    PagingRow["PageText"] = i;
                    if (i <= _TotalPage)
                        PagingTable.Rows.Add(PagingRow);
                }
            }
            else
            {
                for (int i = _TotalPage - _NumberOfPageDisplay + 1; i <= _TotalPage; i++)
                {
                    if (i > 0)
                    {
                        DataRow PagingRow = PagingTable.NewRow();
                        PagingRow["PageIndex"] = i;
                        PagingRow["PageText"] = i;
                        if (i <= _TotalPage)
                            PagingTable.Rows.Add(PagingRow);
                    }
                }
            }
        }
        else
        {
            for (int i = 1; i <= _NumberOfPageDisplay; i++)
            {
                DataRow PagingRow = PagingTable.NewRow();
                PagingRow["PageIndex"] = i;
                PagingRow["PageText"] = i;
                if (i <= _TotalPage)
                    PagingTable.Rows.Add(PagingRow);
            }
        }

        // Binding data
        int SelectedIndex = 0;
        for (int i = 0; i < PagingTable.Rows.Count; i++)
        {
            if (Convert.ToInt32(PagingTable.Rows[i]["PageText"]) == _CurrentPage)
            {
                SelectedIndex = i;
            }
        }
        NumericSectionNumericList.SelectedIndex = SelectedIndex;
        NumericSectionNumericList.DataSource = PagingTable;
        NumericSectionNumericList.DataBind();

        // Setting for numeric pages label 
        lblPagesCurrent.Text = Convert.ToString(_CurrentPage);
        lblPagesTotal.Text = Convert.ToString(_TotalPage);

        // Setting for numeric records label 
        string BeginingRecords = Convert.ToString((CurrentPage - 1) * PageSize + 1);
        string EndingRecords = Convert.ToString(CurrentPage * PageSize > this.TotalRecord ? this.TotalRecord : CurrentPage * PageSize);
        lblRecordsCurrent.Text = BeginingRecords + " - " + EndingRecords;
        lblRecordsTotal.Text = Convert.ToString(this.TotalRecord);

        // Display items based on setting
        this.DisplaySetting();
    }

    public void ReBindPaging()
    {
        this.BindPaging();
    }

    #endregion

    #region Display setting based on paging setting

    protected void DisplaySetting()
    {
        // Paging number setting
        if (this.PagingNumberSetting == PagingNumberType.NumberList)
        {
            NumericSectionNumericList.Visible = true;
            NumericSectionCurrentTotalPage.Visible = false;
            NumericSectionCurrentTotalRecord.Visible = false;
        }
        else if (this.PagingNumberSetting == PagingNumberType.CurrentPageAndTotalPages)
        {
            NumericSectionNumericList.Visible = false;
            NumericSectionCurrentTotalPage.Visible = true;
            NumericSectionCurrentTotalRecord.Visible = false;
        }
        else if (this.PagingNumberSetting == PagingNumberType.CurrentRecordAndTotalRecords)
        {
            NumericSectionNumericList.Visible = false;
            NumericSectionCurrentTotalPage.Visible = false;
            NumericSectionCurrentTotalRecord.Visible = true;
        }
        else
        {
            NumericSectionNumericList.Visible = false;
            NumericSectionCurrentTotalPage.Visible = false;
            NumericSectionCurrentTotalRecord.Visible = false;
            NavigatorObjectNumericContainer.Visible = false;
        }

        // Paging navigator setting
        if (this.PagingNavigatorSetting == PagingNavigatorType.ImageButton)
        {
            ibtnFirst.Visible = true;
            lbtnFirst.Visible = false;
            ibtnPrev.Visible = true;
            lbtnPrev.Visible = false;
            ibtnLast.Visible = true;
            lbtnLast.Visible = false;
            ibtnNext.Visible = true;
            lbtnNext.Visible = false;
        }
        else if (this.PagingNavigatorSetting == PagingNavigatorType.Hyperlink)
        {
            ibtnFirst.Visible = false;
            lbtnFirst.Visible = true;
            ibtnPrev.Visible = false;
            lbtnPrev.Visible = true;
            ibtnLast.Visible = false;
            lbtnLast.Visible = true;
            ibtnNext.Visible = false;
            lbtnNext.Visible = true;
        }
        else
        {
            ibtnFirst.Visible = false;
            lbtnFirst.Visible = false;
            ibtnPrev.Visible = false;
            lbtnPrev.Visible = false;
            ibtnLast.Visible = false;
            lbtnLast.Visible = false;
            ibtnNext.Visible = false;
            lbtnNext.Visible = false;
        }
        // Paging display navigator setting
        if (this.PagingDisplayNavigatorSetting == PagingDisplayNavigatorType.FirstPrevAndNextLast)
        {
            NavigatorObjectFirstContainer.Visible = true;
            NavigatorObjectPrevContainer.Visible = true;
            NavigatorObjectNextContainer.Visible = true;
            NavigatorObjectLastContainer.Visible = true;
        }
        else if (this.PagingDisplayNavigatorSetting == PagingDisplayNavigatorType.PrevAndNext)
        {
            NavigatorObjectFirstContainer.Visible = false;
            NavigatorObjectPrevContainer.Visible = true;
            NavigatorObjectNextContainer.Visible = true;
            NavigatorObjectLastContainer.Visible = false;
        }
        else
        {
            NavigatorObjectFirstContainer.Visible = false;
            NavigatorObjectPrevContainer.Visible = false;
            NavigatorObjectNextContainer.Visible = false;
            NavigatorObjectLastContainer.Visible = false;
        }
    }

    #endregion

    #endregion

    #region Control events -----------------------------

    protected void NumericSectionNumericList_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Paging")
        {
            this.CurrentPage = Convert.ToInt32(e.CommandArgument);
            PageSelectedIndexChange objEventArg = null;
            objEventArg = new PageSelectedIndexChange(this.CurrentPage);
            PageSelectedIndexChanged(this, objEventArg);
            this.BindPaging();
        }
    }

    protected void NumericSectionNumericList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkButtonPage = (LinkButton)e.Item.FindControl("lbtnPaging");
        Table tblItemContentContainer = (Table)e.Item.FindControl("tblItemContentContainer");
        if (lnkButtonPage.CommandArgument.ToString() == this.CurrentPage.ToString())
        {
            //lnkButtonPage.Enabled = false;
            lnkButtonPage.CssClass = this.NumericSectionNumericListHyperlinkSelectedCssClass;
            tblItemContentContainer.CssClass = this.NumericSectionNumericListItemSelectedContainerCssClass;
        }
        else
        {
            lnkButtonPage.CssClass = this.NumericSectionNumericListHyperlinkCssClass;
            tblItemContentContainer.CssClass = this.NumericSectionNumericListItemContainerCssClass;
        }
    }

    protected void ibtnFirst_Click(object sender, ImageClickEventArgs e)
    {
        this.CurrentPage = 1;
        PageSelectedIndexChange objEventArg = null;
        objEventArg = new PageSelectedIndexChange(this.CurrentPage);
        PageSelectedIndexChanged(this, objEventArg);
        this.BindPaging();
    }

    protected void ibtnPrev_Click(object sender, ImageClickEventArgs e)
    {
        this.CurrentPage = (this.CurrentPage - 1) > 0 ? this.CurrentPage - 1 : 1;
        PageSelectedIndexChange objEventArg = null;
        objEventArg = new PageSelectedIndexChange(this.CurrentPage);
        PageSelectedIndexChanged(this, objEventArg);
        this.BindPaging();
    }

    protected void ibtnNext_Click(object sender, ImageClickEventArgs e)
    {
        this.CurrentPage = (this.CurrentPage + 1) > this.TotalPage ? this.TotalPage : (this.CurrentPage + 1);
        PageSelectedIndexChange objEventArg = null;
        objEventArg = new PageSelectedIndexChange(this.CurrentPage);
        PageSelectedIndexChanged(this, objEventArg);
        this.BindPaging();
    }

    protected void ibtnLast_Click(object sender, ImageClickEventArgs e)
    {
        this.CurrentPage = this.TotalPage;
        PageSelectedIndexChange objEventArg = null;
        objEventArg = new PageSelectedIndexChange(this.CurrentPage);
        PageSelectedIndexChanged(this, objEventArg);
        this.BindPaging();
    }

    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        this.CurrentPage = 1;
        PageSelectedIndexChange objEventArg = null;
        objEventArg = new PageSelectedIndexChange(this.CurrentPage);
        PageSelectedIndexChanged(this, objEventArg);
        this.BindPaging();
    }

    protected void lbtnPrev_Click(object sender, EventArgs e)
    {
        this.CurrentPage = (this.CurrentPage - 1) > 0 ? this.CurrentPage - 1 : 1;
        PageSelectedIndexChange objEventArg = null;
        objEventArg = new PageSelectedIndexChange(this.CurrentPage);
        PageSelectedIndexChanged(this, objEventArg);
        this.BindPaging();
    }

    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        this.CurrentPage = (this.CurrentPage + 1) > this.TotalPage ? this.TotalPage : (this.CurrentPage + 1);
        PageSelectedIndexChange objEventArg = null;
        objEventArg = new PageSelectedIndexChange(this.CurrentPage);
        PageSelectedIndexChanged(this, objEventArg);
        this.BindPaging();
    }

    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        this.CurrentPage = this.TotalPage;
        PageSelectedIndexChange objEventArg = null;
        objEventArg = new PageSelectedIndexChange(this.CurrentPage);
        PageSelectedIndexChanged(this, objEventArg);
        this.BindPaging();
    }

    #endregion
}
